package com.mcxx.modules.miniSalvation.fundpay.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.FundPayConvert;
import com.mcxx.modules.miniSalvation.fundpay.dao.write.CustomFundPayMemberWriteDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayMemberDTO;
import com.mcxx.modules.miniSalvation.fundpay.entity.CustomFundPayMemberEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("customFundPayMemberWriteService")
@TaTransactional
public class CustomFundPayMemberWriteService extends BaseService {

    @Autowired
    private CustomFundPayMemberWriteDao customFundPayMemberWriteDao;


    public int inserts(List<CustomFundPayMemberEntity> customFundPayMemberList, String customFundPayId){
        Date createTime = new Date();

        customFundPayMemberList.forEach(entity->{
            entity.setCreateTime(createTime);

            entity.setFundNumber(0d);

            entity.setPersonNumber(1);

            entity.setId(Constant.getUUID());

            entity.setCustomFundPayId(customFundPayId);
        });

        int rst = customFundPayMemberWriteDao.inserts(customFundPayMemberList);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_10040);
        }
        return rst;
    }


    public int insertsExcel(List<CustomFundPayMemberEntity> customFundPayMemberList, Map<String, CustomFundPayMemberDTO> excelMemberMap, String customFundPayId){
        Date createTime = new Date();

        customFundPayMemberList.forEach(entity->{

            CustomFundPayMemberDTO dto = excelMemberMap.get(entity.getIdcard());

            entity.setCreateTime(createTime);

            entity.setFundNumber(0d);

            entity.setPersonNumber(1);

            entity.setId(Constant.getUUID());

            entity.setCustomFundPayId(customFundPayId);

            entity.setFundNumber(dto.getFundNumber());

            entity.setRemark(dto.getRemark());
        });

        int rst = customFundPayMemberWriteDao.inserts(customFundPayMemberList);
        if(rst <= 0){
            throw new BizException("-1", "成员添加失败,数据库操作异常");
        }
        return rst;
    }


    public int deleteByCustomFundPayIdAndMemberId(String customFundPayId, List<String> memberIdList){
        int rst = customFundPayMemberWriteDao.deleteByCustomFundPayIdAndMemberId(customFundPayId, memberIdList);
        if(rst <= 0){
            throw new BizException("-1", "名册成员删除失败");
        }
        return rst;
    }


    public int updateById(CustomFundPayMemberDTO customFundPayMemberDTO){

        CustomFundPayMemberEntity customFundPayMemberEntity = FundPayConvert.convertCustomFundPayMemberEntity(customFundPayMemberDTO);

        int rst = customFundPayMemberWriteDao.updateById(customFundPayMemberEntity);
        if(rst <= 0){
            throw new BizException("-1", "成员信息修改失败");
        }
        return  rst;
    }
}
